﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Wicresoft.SMS.Common
{
    public static class Logger
    {
        private static string CreateLogger()
        {
            string logDirFormat = null;
            string logPathFormat = null;
            if (Environment.OSVersion.Platform == PlatformID.Win32NT)
            {
                logDirFormat = @"{0}\Wicresoft EKA\smsWinService\Logs";
                logPathFormat = @"{0}\Wicresoft EKA\smsWinService\Logs\{1}.log";
            }
            else if (Environment.OSVersion.Platform == PlatformID.Unix)
            {
                logDirFormat = "{0}/Wicresoft EKA/smsWinService/logs";
                logPathFormat = "{0}/Wicresoft EKA/smsWinService/logs/{1}.log";
            }

            var dir = String.Format(logDirFormat,
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            var log = String.Format(logPathFormat,
                Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
                DateTime.Today.ToString("yyyy-MM-dd"));

            // for windows, it should return something like
            //  C:\ProgramData\Wicresoft\AppLab\Logs\xxx.log
            // while for linux, it should return something like
            //  /usr/share/wicresoft/applab/logs/xxx.log
            return log;
        }
        private static object obj = null;


        /// <summary>
        /// log info to log txt
        /// </summary>
        /// <param name="ouputString"></param>
        public static void Info(string ouputString)
        {
            if (obj == null)
            {
                obj = new object();
                Log.Listeners.Add(new TextWriterLogListener(CreateLogger()));
            }
            Log.WriteInfo(ouputString);
        }

        /// <summary>
        /// log error to log txt
        /// </summary>
        /// <param name="ouputString"></param>
        public static void Error(string ouputString)
        {
            if (obj == null)
            {
                obj = new object();
                Log.Listeners.Add(new TextWriterLogListener(CreateLogger()));
            }
            Log.WriteError(ouputString);
        }

        /// <summary>
        /// log debug to log txt
        /// </summary>
        /// <param name="ouputString"></param>
        public static void Debug(string ouputString)
        {
            if (obj == null)
            {
                obj = new object();
                Log.Listeners.Add(new TextWriterLogListener(CreateLogger()));
            }
            Log.WriteDebug(ouputString);
        }
    }
}
